package com.localization;
import android.util.Log;

import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.DatagramChannel;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;

/**
 * Created by Edward on 2017/1/7.
 */
public class SocketUdpClient implements SocketClientInterface
{
    private Gateway gateway = null;
    private Selector selector = null;
    private DatagramChannel udpChannel = null;
    private ByteBuffer Read_byteBuffer = null;
    private ByteBuffer Send_byteBuffer = null;
    private  InetSocketAddress address = null;
    private GatawayBuffer RxBuffer = new GatawayBuffer();

    public SocketUdpClient(Gateway gateway)
    {
        System.out.println("SocketUdpClient");
        this.gateway = gateway;
        this.selector = gateway.getSelector();
        Read_byteBuffer = ByteBuffer.allocate(512);
        Send_byteBuffer = ByteBuffer.allocate(512);
    }

    @Override
    public void connect(String MAC,String url,int port)
    {
        System.out.println("connect");
        try {
            /*打开一个UDP*/
            this.udpChannel = DatagramChannel.open();
            /*设置UDP noblocking*/
            this.udpChannel.configureBlocking(false);
            /*设置UDP监听端口*/
            this.udpChannel.socket().bind(new InetSocketAddress(port));
            /*设置UDP目标地址*/
            this.address = new InetSocketAddress(url,port);

//            this.udpChannel.connect(address);
            this.udpChannel.register(this.selector,SelectionKey.OP_READ);
            this.gateway.register(MAC,this.udpChannel,this);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public void close()
    {
        try {
            udpChannel.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    /*数据处理*/
    @Override
    public void Process(SelectionKey key)
    {
        DatagramChannel channel = (DatagramChannel) key.channel();
        if(this.udpChannel.equals(channel))
        {
            try{
                InetSocketAddress address = (InetSocketAddress) channel.receive(Read_byteBuffer);
                RxBuffer.setBuffer(Read_byteBuffer.array(),Read_byteBuffer.position());



               HostCmd mHostCmd = Utils.CmdPorcess(RxBuffer);

                if(mHostCmd != null)
                {
                    SocketCmdsDistribute.CmdsDistribute(mHostCmd);
                }
                Read_byteBuffer.clear();
            }catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public int send(byte[] cmd)
    {
        int byteToSend = 0;
        Send_byteBuffer.clear();
        Send_byteBuffer.put(cmd);
        Send_byteBuffer.flip();

        try {
            if(this.udpChannel.isOpen()){
            byteToSend = this.udpChannel.send(Send_byteBuffer, address);
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
        return byteToSend;
    }
}
